package cn.com.callback

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.com.elite.EliteBean;
import cn.com.elite.PropertiesUtil;
import cn.com.elite.Util;
import java.util.Map;
import java.util.HashMap;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.regex.Matcher
import java.util.regex.Pattern

public class SmsGetMoMwyx implements CallBackInvoke{
	private static final Log log=LogFactory.getLog(SmsGetMoMwyx.class);
	public void doCallBack(EliteBean eliteBean,String callBack,Map map,String body){
		log.debug("=========================MWYXGetMo==================");
		PropertiesUtil pt= new PropertiesUtil();
		def timestamp = Util.getNowString("MMddHHmmss");
		def userid = pt.readValue("mwyx_userid"); //短信通道账号
		def pwd = Util.getMD5Str(userid+pt.readValue("sms_str")+pt.readValue("mwyx_pwd")+timestamp); //userid + 固定字符串+ pwd(明文) + 时间戳，再进行MD5加密
		def ipport = pt.readValue("sms_ip")+":"+pt.readValue("mwyx_port"); //短信通道ip+端口
		def url = "http://"+ipport+"/sms/v2/std/get_mo"; //接口请求地址
		//log.debug("\n"+"userid:"+userid+"\n"+"pwd:"+userid+pt.readValue("sms_str")+pt.readValue("mwyx_pwd")+timestamp+"\n"+"pwd:"+pwd+"\n"+"url:"+url)
		def charset = "utf-8"; //设置字符集
		def timeout = 100000; //设置请求超时时间
		def enableSSLflag = 0;
		Map headMap=new HashMap(); //设置头文件
		headMap.put("Content-Type","application/json");
		def i = 0;
		def mobile = "";
		def spno = 0;
		def exno = 0;
		def msg = "";
		def smap = "";
		def sk_invpty = 0;
		def input = "";
		def sqlresult = "";
		def guid = "";
		try {
			JSONObject josnbody = new JSONObject();
			josnbody.put("userid",userid);
			josnbody.put("pwd",pwd);
			josnbody.put("timestamp",timestamp);
			josnbody.put("retsize",200);
			def msgjson = Util.doHttpPostUseHead(url,JSONObject.fromObject(josnbody).toString(),charset,timeout,enableSSLflag,headMap);//接口返回报文
			//log.debug("result:"+msgjson);
			JSONObject obj = JSONObject.fromObject(msgjson);
			if(obj.get("result")==0) {
				JSONArray mos = obj.getJSONArray("mos");
				for (i = 0; i < mos.size(); i++) {
					try {
						JSONObject it = mos.get(i);
						//log.debug("it:"+it);
						mobile = it.get("mobile");
						spno = Util.filterForObject(it.get("spno")).equals("")?0:it.get("spno");
						exno = Util.filterForObject(it.get("exno")).equals("")?0:it.get("exno");
						msg = URLDecoder.decode(it.get("content"), "UTF-8");
						guid = Util.getGuid();
						eliteBean.update("insert into sms_getmo(rec_guid,mobile,spno,exno,rtime,msg,pipe)values('"+guid+"','"+mobile+"',"+spno+","+exno+",to_date('"+it.get("rtime")+"','yyyy-mm-dd hh24:mi:ss'),'"+msg+"',1)","dataSource");
						msg = replaceBlank(msg); //去空格去回车符处理
						msg = msg.toUpperCase(); //字符串转大写处理
						if(msg.equals("T") || msg.equals("TD")) {
							smap = eliteBean.chaxun_sql("select sk_invpty from sms_send where phone='"+mobile+"' and exno="+exno+"","dataSource");
							if(smap.size()==0) {
								continue;
							};
							sk_invpty = smap[0].get("sk_invpty");
						}else{
							continue;
						};
						eliteBean.update("insert into sms_unsubscribe(sk_invpty,phone,exno,spno) select "+sk_invpty+",'"+mobile+"',"+exno+",'"+spno+"' from dual left join sms_unsubscribe on sk_invpty="+sk_invpty+" where sk_invpty is null","dataSource");
						eliteBean.update("update sms_getmo set checkstate=2,checkdesc='系统已自动处理' where rec_guid='"+guid+"'","dataSource");
					}catch(Exception e) {
						log.error("ErrorMessage:",e);
					};
				};	
			};
		}catch(Exception e) {
			log.error("ErrorMessage:",e);
		};
	}
	//字符串去空格去回车符处理
	public static String replaceBlank(String str) {
		String dest = "";
		if (str!=null) {
			Pattern p = Pattern.compile("\\s*|\t|\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}
}
